import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import Detail from '@/views/Detail.vue'
import Layout from '@/views/Layout.vue'

import Article from '@/views/Article.vue'
import Collect from '@/views/Collect.vue'
import Like from '@/views/Like.vue'
// import User from @/views/User.vue
const User = () => import('@/views/User.vue')
import { getToken } from '@/utils/storage'
// import { from } from 'core-js/core/array'

Vue.use(VueRouter)

const routes = []

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/detail/:id', component: Detail },
    {
      path: '/',
      component: Layout,
      redirect: '/article',
      children: [
        { path: 'article', component: Article },
        { path: 'collect', component: Collect },
        { path: 'like', component: Like },
        { path: 'user', component: User },
      ]
    }
  ]
})

// 白名单
const whiltList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()

  if (token || whiltList.includes(to.path)) {
    next()
  } else {
    next('/login')
  }
})

export default router
